package com.yangrd.airship.generator.config;

import com.yangrd.airship.generator.PageGeneratorService;
import com.yangrd.airship.generator.page.enhance.PageBaseColumnRegistry;
import com.yangrd.airship.generator.page.PageGeneratorDefineFactory;
import com.yangrd.airship.generator.page.enhance.TranslateHelper;
import com.yangrd.airship.generator.page.impl.CacheTranslateHelper;
import com.yangrd.airship.generator.page.impl.SimplePageBaseColumnRegistry;
import com.yangrd.airship.generator.page.impl.factory.StrPageGeneratorDefineFactory;
import com.yangrd.airship.generator.page.impl.YouDaoTranslateHelper;
import com.yangrd.airship.generator.page.impl.factory.clazz.ClassPageGeneratorDefineFactory;
import com.yangrd.airship.generator.support.PageGeneratorDefineMetaHook;
import com.yangrd.airship.generator.support.PageGeneratorWriter;
import com.yangrd.airship.generator.support.PageTemplateResolver;
import com.yangrd.airship.generator.support.impl.CompositePageGeneratorWriter;
import com.yangrd.airship.generator.support.impl.ConsolePageGeneratorWriter;
import com.yangrd.airship.generator.support.impl.FilePageGeneratorWriter;
import com.yangrd.airship.generator.support.impl.FreemarkerPageTemplateResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.List;

/**
 * @author yangrd
 * @date 2021/11/8
 */
@Configuration
public class PageGeneratorConfig {

    @Bean
    public PageBaseColumnRegistry pageBaseColumnRegistry() {
        return new SimplePageBaseColumnRegistry();
    }


    @Bean
    public TranslateHelper youDaoTranslateHelper() {
        return new YouDaoTranslateHelper();
    }

    @Bean
    @Primary
    public TranslateHelper translateHelper(List<TranslateHelper> translateHelpers) {
        return new CacheTranslateHelper(translateHelpers);
    }

    @Bean
    public StrPageGeneratorDefineFactory strPageGeneratorDefineFactory(PageBaseColumnRegistry pageBaseColumnRegistry, TranslateHelper translateHelper) {
        return new StrPageGeneratorDefineFactory(pageBaseColumnRegistry, translateHelper);
    }

    @Bean
    public ClassPageGeneratorDefineFactory classPageGeneratorDefineFactory( TranslateHelper translateHelper) {
        return new ClassPageGeneratorDefineFactory( translateHelper);
    }

    @Bean
    public PageGeneratorWriter consolePageGeneratorWriter() {
        return new ConsolePageGeneratorWriter();
    }

    @Bean
    @ConditionalOnExistProperty(prefix = PageGeneratorProperties.PREFIX, value = "out-file-path")
    public PageGeneratorWriter filePageGeneratorWriter(PageGeneratorProperties properties) {
        return new FilePageGeneratorWriter(properties.getOutFilePath());
    }

    @Bean
    @Primary
    public PageGeneratorWriter pageGeneratorWriter(List<PageGeneratorWriter> writers) {
        return new CompositePageGeneratorWriter(writers);
    }

    @Bean
    public PageTemplateResolver pageTemplateResolver(PageGeneratorDefineMetaHook pageGeneratorDefineMetaHook) {
        return new FreemarkerPageTemplateResolver(pageGeneratorDefineMetaHook);
    }

    @Bean
    public PageGeneratorService pageGeneratorService(PageTemplateResolver pageTemplateResolver, PageGeneratorWriter pageGeneratorWriter) {
        return new PageGeneratorService(pageTemplateResolver, pageGeneratorWriter);
    }
}
